$(function() {
    //手机验证&Email验证
    var InterValObj,emInterValObj,btnInterValObj; //timer变量，控制时间
    var count = 60; //间隔函数，1秒执行
    var emcount = 60;
    var btncount=60;
    var curCount,emcurCount;//当前剩余秒数
    var button=$('.yzbtn');
    var embutton=$('.checkemail');
    var cellbutton=$('#getCode');
    $('.yzbtn').click(function () {

        curCount = count;
        var mobile = $("input[name='mobile']").val();
        console.log(mobile);
        var myreg = /^(((13[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        if (!myreg.test(mobile)) {
            console.log(123);
            alert('请输入有效的手机号码！');
            return false;
        }
        $('.yzbtn').attr("disabled", "true");
        $.post(smsUrl,{mobile:mobile},function(result){
            if(result.code=='0000'){
                alert(result.message);
                $('.yzbtn').html(curCount + '秒后重新发送验证码')
                InterValObj = window.setInterval(SetRemainTime, 1000)
                $('.mbcode').css('display', 'block');
            }else{
                alert(result.message);
            }
        })


    });
    $('#getCode').click(function () {
        BtncurCount = btncount;
        var mobile = $("input[name='mobile']").val();
        console.log(mobile);
        var myreg = /^(((13[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        if (!myreg.test(mobile)) {
            console.log(123);
            alert('请输入有效的手机号码！');
            return false;
        }
        $('#getCode').attr("disabled", "true");
        $.post(smsUrl,{mobile:mobile},function(result){
            if(result.code=='0000'){
                alert(result.message);
                $('#getCode').html(BtncurCount + '秒后重新发送验证码')
                btnInterValObj = window.setInterval(btnSetRemainTime, 1000)
                $('.mbcode').css('display', 'block');
            }else{
                alert(result.message);
            }
        })


    });
    $('.checkemail').click(function () {
        emcurCount = emcount;
        var email = $("input[name='email']").val();
        console.log(email);
        if (email=='') {
            console.log(123);
            layer.alert('请输入正确邮箱地址！');
            return false;
        }
        $('.checkemail').attr("disabled", "true");
        $.post(emailUrl,{to:email,subject:'邮箱验证码',desc:'register'},function(result){
            if(result.code='0000'){
                layer.alert(result.message);
                $('.checkemail').html(emcurCount + '秒后重新发送验证码')
                emInterValObj = window.setInterval(emSetRemainTime, 1000)
                //$('.emcode').css('display', 'block');
            }else{
                layer.alert(result.message);
            }
        })
    });
    function SetRemainTime() {
        if (curCount == 0) {
            window.clearInterval(InterValObj);//停止计时器
            button.removeAttr("disabled");//启用按钮
            button.html("重新发送验证码");
        }
        else {
            curCount--;
            button.html(curCount + '秒后重新发送验证码');
        }
    }
    function btnSetRemainTime() {
        if (BtncurCount == 0) {
            window.clearInterval(btnInterValObj);//停止计时器
            cellbutton.removeAttr("disabled");//启用按钮
            cellbutton.html("重新发送验证码");
        }
        else {
            BtncurCount--;
            cellbutton.html(BtncurCount + '秒后重新发送验证码');
        }
    }
    function emSetRemainTime() {
        if (emcurCount == 0) {
            window.clearInterval(emInterValObj);//停止计时器
            embutton.removeAttr("disabled");//启用按钮
            embutton.html("重新发送验证码");
        }
        else {
            emcurCount--;
            embutton.html(emcurCount + '秒后重新发送验证码');
        }
    }
});